<template>
  <base-breadcrumb>
    <searchData
      :search-data="searchArr"
      @search="search"
    />
    <a-card>
      <div class="table-operator">
        <a-button
          v-btnPermission="'tm_011_add_01'"
          type="primary"
          icon="plus"
          @click="handleUpdate('add', {})"
        >
          新增会议
        </a-button>
      </div>
      <div class="pub-table-select-count">
        <a-icon
          type="info-circle"
          theme="twoTone"
        />
        <span>专业分包方案数：</span>
        <a>{{ totalInfo.proAllNum || 0 }}</a>
        <span>{{ ' | ' }}已讲评方案数：</span>
        <a>{{ totalInfo.completeNum || 0 }}</a>
        <span>{{ ' | ' }}未讲评方案数：</span>
        <a>{{ totalInfo.proAllNum - totalInfo.completeNum || 0 }}</a>
        <span>{{ ' | ' }}专业分包方案讲评率：</span>
        <a>{{ calcRate(totalInfo.completeNum, totalInfo.proAllNum) }}%</a>
      </div>
      <list-table
        ref="table"
        size="default"
        row-key="key"
        :columns="columns"
        :request="loadData"
        :alert="true"
        show-size-changer
        show-quick-jumper
        show-pagination="auto"
      >
        <span
          slot="operation"
          slot-scope="text, record"
        >
          <MoreButton>
          <a
            v-btnPermission="'tm_011_loo_03'"
            @click="handleUpdate('start', record)"
            v-if="record.status === 20"
          >查看会议</a>
                    <a
                            v-if="record.status === 30 || record.status === 40 "
                            v-btnPermission="'tm_011_loo_03'"
                            @click="handleUpdate('look', record)"
                    >查看会议</a>
          <a
            v-if="record.status == 30"
            v-btnPermission="'tm_011_edi_02'"
            @click="handleUpdate('edit', record)"
          >进入会议</a>
            </MoreButton>
        </span>
        <span
          slot="commentedPlan"
          slot-scope="text, record"
        >
          {{ record.meetingExtraVO.commentedPlan }}
        </span>
        <span
          slot="professionComName"
          slot-scope="text, record"
        >
          {{ record.meetingExtraVO.professionComName }}
        </span>
<!--        <span-->
<!--          slot="avgScore"-->
<!--          slot-scope="text, record"-->
<!--        >-->
<!--          {{ record.meetingExtraVO.avgScore }}-->
<!--        </span>-->

        <span
          slot="status"
          slot-scope="text, record"
        >
          <list-status
            :status="record.status"
            conversion="en"
            :text="text"
          />
        </span>
      </list-table>
    </a-card>
  </base-breadcrumb>
</template>
<script>
import SelectSubCompany from '@/components/Select/SelectSubCompany' // 分公司
import SelectProject from '@/components/Select/SelectProject' // 项目
import OrgTreeSelect from '@/components/OrgTreeSelect'
import SelectSubcontractor from '@/components/Select/SelectSubcontractor' // 分包商
import { STable } from '@/components'
import listStatus from '@/components/list_status/meeting_status'
import { getMeetingList, startMeeting, getSearchList } from '@/api/office/conference'
import { getmeetingTotal } from '@/api/technical/meeting.js'

const columns = [
    {
        title: '所属分公司',
        dataIndex: 'branchComName',
    },
    {
        title: '所属项目',
        dataIndex: 'projectName',
    },
    {
        title: '方案名称',
        dataIndex: 'commentedPlan',
        scopedSlots: { customRender: 'commentedPlan' },
    },
    {
        title: '评审得分',
        dataIndex: 'commentedScore',
        // scopedSlots: { customRender: 'avgScore' },
    },
    {
        title: '专业单位名称',
        dataIndex: 'professionComName',
        scopedSlots: { customRender: 'professionComName' },
    },
    {
        title: '主持人',
        dataIndex: 'speaker',
    },
    {
        title: '召开时间',
        dataIndex: 'startTime',
    },
    {
        title: '状态',
        dataIndex: 'status',
        scopedSlots: { customRender: 'status' },
    },

    {
        title: '操作',
        dataIndex: 'operation',
        width: '170px',
        scopedSlots: { customRender: 'operation' },
    },
]
const searchArr = [
    // {
    //     name: '所属分公司',
    //     valueKey: 'branchComId',
    //     type: SelectSubCompany,
    // },
    {
          name: '所属单位',
          keyValue: 'projectId',
          valueKey: 'searchQuery',
          type: OrgTreeSelect,
        },
    // {
    //     name: '所属项目',
    //     keyValue: 'branchComId',
    //     valueKey: 'projectId',
    //     type: SelectProject,
    // },
    {
        name: '方案名称',
        value: 'commentedPlan',
        type: 'select',
        data: [],
    },
    {
        name: '分包商名称',
        keyValue: 'projectId',
        valueKey: 'subContractorId',
        type: SelectSubcontractor,
    },

    {
        name: '讲解人',
        value: 'speakerName',
        type: 'input',
    },
    {
        name: '状态',
        value: 'status',
        type: 'select',
        data: [
            {
                value: '20',
                label: '待开始',
            },
            {
                value: '30',
                label: '进行中',
            },

            {
                value: '40',
                label: '已完结',
            },
        ],
    },
    {
        name: '填报人',
        value: 'creatorName',
        type: 'input',
    },
    {
        name: '填报时间',
        value: 'startEndDate',
        type: 'dateRange',
    },
]

export default {
    name:'a'+Date.now(),    components: {
        STable,
        listStatus,
    },
    data() {
        this.columns = columns

        return {
            totalInfo: {}, // 统计信息
            searchArr,
            queryParam: {},
            loadData: async (parameter) => {
                this.queryParam.query = { ...this.queryParam.query, typeId: 2 }
                const requestParameters = Object.assign({}, parameter, this.queryParam)
                this.getmeetingTotalReq(requestParameters) // 调用统计
                const [err, res] = await getMeetingList(requestParameters)
                // this.getMeetingListCountReq(this.queryParam)
                return new Promise((resolve, reject) => {
                    if (err) {
                        reject(err)
                    } else {
                        resolve(res)
                    }
                })
            },
        }
    },
    mounted() {
        this.getSearchListReq()
    },
    methods: {
        /**
         * 计算专业分包方案讲评率
         * @param {Number} a 除数
         * @param {Number} b 被除数
         */
        calcRate(a,b) {
            const rate = a / b
            if(Number.isFinite(rate)) {
                return (rate * 100).toFixed(2)
            }
            return '0.00'
        },
      /**
         * 获取统计信息
         */
        async getmeetingTotalReq(extra = {}) {
            try {
                const params = {
                    ...extra,
                    ...extra.query,
                    typeId: 2
                }
                const res = await getmeetingTotal(params)
                this.totalInfo = res.data || {}
                this.totalInfo.rate = (this.totalInfo.rate * 100).toFixed(2)
            } catch(e) {
                console.error(e)
            }
        },
        async getSearchListReq() {
            const [err, res] = await getSearchList({ typeId: 2 })
            if (!err) {
                res.data.forEach((item) => {
                    item.value = item.id
                    item.label = item.name
                })
                this.searchArr.find(item => item.value === 'commentedPlan').data = res.data || []
            }
        },
        //搜索
        search(val = {}) {
            this.queryParam.query = {
                ...val,
                typeId: 2,
                hsemonth: (val.month && val.month.format('YYYY-MM')) || void 0,
                startTime:
                    (val.startEndDate && val.startEndDate.length && val.startEndDate[0].format('YYYY-MM-DD')) || void 0,
                endTime:
                    (val.startEndDate && val.startEndDate.length && val.startEndDate[1].format('YYYY-MM-DD')) || void 0,
            }
            console.log(this.queryParam)

            this.$refs.table.refresh(true)
        },

        // 处理更新操作（新增、编辑）
        async handleUpdate(type, data = {}) {
            if (type == 'del') {
                // const [err, res] = await deleteEquipment({ id: data.id })
                // if (!err) {
                //     this.$message.success('成功')
                //     this.$refs.table.refresh(true)
                // }
            }
            // else if (type == 'start') {
            //     const [err, res] = await startMeeting({ id: data.id })
            //     if (!err) {
            //         this.$message.success('成功')
            //         this.$refs.table.refresh(true)
            //     }
            // }
            else if (type == 'edit' || type == 'look' || type == 'start') {
                this.$router.push({
                    name: 'evaluationMeetingCheck',
                    query: {
                        id: data.id || '',
                        pageType: type,
                    },
                })
            } else {
                this.$router.push({
                    name: 'evaluationMeetingAddConference',
                    query: {
                        id: data.id || '',
                        pageType: type,
                        typeId: 2,
                    },
                })
            }
        },
    },
}
</script>
